Skip to content

feature: add AI-powered Git diff analysis for working tree and commit range comparison#2457

Open
YuHaoLiaoTony wants to merge 2 commits into
sourcegit-scm:developfrom
YuHaoLiaoTony:tony/AIDiffContext
Open

feature: add AI-powered Git diff analysis for working tree and commit range comparison#2457
YuHaoLiaoTony wants to merge 2 commits into
sourcegit-scm:developfrom
YuHaoLiaoTony:tony/AIDiffContext

Conversation

@YuHaoLiaoTony

@YuHaoLiaoTony YuHaoLiaoTony commented Jun 18, 2026

Copy link
Copy Markdown

This PR adds an AI-powered Git diff analysis feature that lets users get a natural-language summary of their changes using their configured OpenAI-compatible AI service.

What's new

  • Working tree analysis: Analyze staged + unstaged changes directly from the commit message toolbar
  • Commit range analysis: Select two commits in History and analyze the diff between them, accessible via right-click context menu or the Revision Compare toolbar button
  • Locale-aware AI output: Automatically maps the app's UI locale (16 languages) into the AI prompt, so the summary is generated in the user's language
  • Large diff protection: Automatically truncates diffs exceeding 100K characters or 3K lines; individual files over 20K chars are skipped
  • Binary/LFS skipping: Binary files and LFS pointers are automatically detected and excluded from the analysis payload
  • Markdown rendering: AI output is rendered with proper heading, code block, and inline code formatting using a new MarkdownResultView control
  • Model selection: The analysis dialog includes a model ComboBox matching the existing AI commit message UX, disabled during analysis to prevent mid-request model switching

Architecture

All new code follows the existing MVVM and AI module patterns:

  • AI/ layer: AIDiffContextData (data model), AIDiffContextBuilder (git command execution with SHA validation), DiffPrompts (prompt templates + locale mapping), DiffAgent (thin API call wrapper)
  • ViewModels/AIDiffAnalysis.cs: state management, cancellation, retry, error mapping
  • Views/AIDiffAnalysis.axaml + MarkdownResultView.axaml: popup dialog with Markdown rendering

Existing file changes

Minimal modifications to 7 existing files:

  • RevisionCompare.cs: exposed GetSHA() and Repository property (internal → public)
  • CommitMessageToolBox.axaml + .cs: one toolbar button + handler
  • Histories.axaml.cs: context menu item for 2-commit selection + helper
  • RevisionCompare.axaml + .cs: one title bar button + handler
  • AIAssistant.axaml.cs: enabled WordWrap on AIResponseView
  • en_US.axaml + 14 locale files: 18 new localization keys each

Verification

  • dotnet build succeeds (0 errors, 0 warnings)
  • No AI service configured → user notification shown
  • No changes to analyze → user notification shown
  • Single AI service → dialog opens directly
  • Multiple AI services → service selection menu shown
  • Binary/LFS files → skipped with mention in output
  • Large diffs → truncated to stat-only mode

@YuHaoLiaoTony YuHaoLiaoTony changed the title feature: add AI-powered Git diff analysis for working tree and commit… feature: add AI-powered Git diff analysis for working tree and commit range comparison Jun 18, 2026
@love-linger

Copy link
Copy Markdown
Collaborator

Since editors in SourceGit are read-only, I do not recommand users to do this job in SourceGit.

For my part, I have created two custom actions (using VSCode to do code review):

  • One targets uncommitted changes (SCOPE: Repository), with the command: code chat "Review all uncommitted changes in this repo, summarize and flag potential bugs"
  • The other is for selected commits (SCOPE: Commit), with the command: code chat "Review commit ${SHA} in this repo, summarize and flag potential bugs"

@love-linger

Copy link
Copy Markdown
Collaborator
image

@YuHaoLiaoTony

YuHaoLiaoTony commented Jun 18, 2026

Copy link
Copy Markdown
Author

My implementation focuses on a different use case:

  • Quick understanding of someone else's changes (commit browsing / code review)
  • Range comparison (commit A..B diff summary) — not possible with ${SHA} alone in Custom Actions
  • Instant result inside SourceGit, no need for an external editor

Custom Actions with VSCode are great for self-review, but they can't cover these scenarios.

… range comparison

- Add AIDiffContextBuilder with safe git command execution, SHA validation,
  large diff protection (100K chars / 3K lines), binary/LFS file skipping
- Add DiffPrompts with 16-locale language mapping and two prompt templates
- Add DiffAgent as thin AI API call layer reusing existing AI.Service
- Add AIDiffAnalysis ViewModel with analysis state, cancellation, retry
- Add model selection ComboBox matching existing AI commit message UX
- Add MarkdownResultView to render AI output (headings, code, inline code)
- Add working tree analysis button in CommitMessageToolBox toolbar
- Add two-commit analysis via History context menu and RevisionCompare toolbar
- Add 18 locale keys across all 15 locale files
- Fix AIAssistant AIResponseView to enable WordWrap
- **DiffAgent.cs**: 新增 `AnalyzeStreamingAsync` 方法,支援即時串流接收 AI 回覆片段
- **AIDiffAnalysis.cs**: 將 `AnalyzeDiff` 與 `SummarizeChanges` 改為使用串流 API,即時更新結果內容;處理無內容生成情況;將錯誤訊息與狀態更新納入 UI 執行緒派送
- **AIDiffAnalysis.axaml**: 移除 MarkdownResultView 的 `IsVisible` 繫結,使串流輸出過程中即可顯示逐步結果
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants